<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/toulogo.jpg" alt="" />
      </div>
      <div class="login_option">
          <el-select v-model="value" placeholder="请选择身份">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </div>
      <!-- 登陆表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        label-width="0px"
        class="login_form"
        :rules="loginFormRules"
      >
       
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="tourists_index">游客</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登陆表单数据绑定对象
      loginForm: {
        username: "",
        password: "",
      },
      options: [{
          value: '1',
          label: '管理员'
        }, {
          value: '2',
          label: '教师'
        }, {
          value: '3',
          label: '辅导员'
        }, {
          value: '4',
          label: '学生'
        }],
        value: '',
      // 表单验证
      loginFormRules: {
        // 验证用户名
        username: [
          { required: true, message: "请输入登陆账户", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码
        password: [
          { required: true, message: "请输入登陆密码", trigger: "blur" },
          { min: 6, message: "最少6位字符", trigger: "blur" },
        ],
      },
      
    };
  },
  methods: {
    // 表单提交预校验
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        // 发起登陆请求
        const { data: res } = await this.$http.post("login", this.loginForm);
        console.log(res);

        if (res.meta.status !== 200){
          return console.log("登录失败"), this.$message.error("登录失败");
        }
        else  {
          if ( this.value == "1" ){
            console.log("登录成功"), this.$message.success("管理员登录成功");
          // 将登陆成功的之后的token保存在客户端的sessionStorage中
          // 项目中的其他接口，必须在登陆后才能访问
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/admin_index");
          }
          else if(this.value == "2"){
            console.log("登录成功"), this.$message.success("教师登录成功");
          // 将登陆成功的之后的token保存在客户端的sessionStorage中
          // 项目中的其他接口，必须在登陆后才能访问
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/teacher_index");
          }
          else if(this.value == "3"){
            console.log("登录成功"), this.$message.success("辅导员登录成功");
          // 将登陆成功的之后的token保存在客户端的sessionStorage中
          // 项目中的其他接口，必须在登陆后才能访问
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/instructor_index");
          }
          else if(this.value == "4"){
            console.log("登录成功"), this.$message.success("学生登录成功");
          // 将登陆成功的之后的token保存在客户端的sessionStorage中
          // 项目中的其他接口，必须在登陆后才能访问
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/student_index");
          }
          else{
            this.$message.error("请选择身份")
          }
          
          
        }

        
        

      }

      );

    },

    tourists_index(){
      this.$router.push("/tourists_index");
    }
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 370px;
  background-color: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #ffffff;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 90%;
  padding: 0 20px;
  padding-left: 60px;
  box-sizing: border-box;
}
.btns {
  padding-bottom: 8px;
  display: flex;
  justify-content: center;
}
.login_option{
  position: absolute;
  left: 50%;
  top: 35%;
  transform: translate(-50%, -50%);
  
}
</style>

